<?php

namespace Console\Supports;

use Exception;
use Illuminate\Support\Facades\DB;

class Helper
{
    /**
     * 创建文件时，根据目标名称解析 目标文件名、命名空间名、类名
     */
    public static function generateFilePath(string $className, string $baseFileName, bool $isCreate = true)
    {
        $className = str_replace('\\', '/', $className);
        $className = trim($className, '/');
        $className = ucfirst($className);

        if (strpos($className, '-') !== false) {
            $className = array_map(function ($v) {
                return ucfirst($v);
            }, explode('-', $className));
            $className = implode('', $className);
        }
        if (strpos($className, '/') !== false) {
            $className = array_map(function ($v) {
                return ucfirst($v);
            }, explode('/', $className));
            $className = implode('/', $className);
        }

        $fileName = ROOT_PATH . $baseFileName . DIRECTORY_SEPARATOR;

        if (strpos($className, '/') !== false) {
            $path = substr($className, 0, strrpos($className, '/'));
            $className = substr($className, strrpos($className, '/') + 1);

            $fileName .= $path . DIRECTORY_SEPARATOR;

            $path = str_replace('/', '\\', $path);
            $path = '\\' . $path;
        } else {
            $path = '';
        }

        $fileName .= $className . '.php';
        if (file_exists($fileName)) {
            throw new Exception('class file is exist');
        } else {
            if ($isCreate === true && !is_dir(dirname($fileName))) {
                mkdir(dirname($fileName), 0777, true);
            }
        }

        return [$className, $path, $fileName];
    }

    /**
     * 获取数据迁移文件的根目录路径
     */
    public static function migrateFileBasePath(): string
    {
        return ROOT_PATH . 'console' . DIRECTORY_SEPARATOR . 'Migrations';
    }

    /**
     * 获取迁移批次
     */
    public static function getCurrentBatch(): int
    {
        $infoRs = DB::table('migrations')->select('id', 'batch')
            ->orderBy('id', 'desc')->first();
        if (!isset($infoRs->id)) {
            return 1;
        } else {
            return $infoRs->batch + 1;
        }
    }

    /**
     * 迁移文件名转换为类名
     */
    public static function getMigrateFileClass(string $fileName): string
    {
        $tmp = substr($fileName, 0, 4);
        if (!is_numeric($tmp)) {
            return '';
        }
        $fileName = substr($fileName, 5);
        $tmp = substr($fileName, 0, 2);
        if (!is_numeric($tmp)) {
            return '';
        }
        $fileName = substr($fileName, 3);
        $tmp = substr($fileName, 0, 2);
        if (!is_numeric($tmp)) {
            return '';
        }
        $fileName = substr($fileName, 3);
        $tmp = substr($fileName, 0, 6);
        if (!is_numeric($tmp)) {
            return '';
        }
        $fileName = substr($fileName, 7);
        $fileName = array_map(function ($v) {
            return ucfirst($v);
        }, explode('_', $fileName));

        $fileName = implode('', $fileName);
        return $fileName;
    }

    /**
     * 获取数据填充文件的根目录路径
     */
    public static function seederFileBasePath(): string
    {
        return ROOT_PATH . 'console' . DIRECTORY_SEPARATOR . 'Seeders';
    }
}
